package com.example.xiaoyuan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xiaoyuan.pojo.Student;
import com.example.xiaoyuan.service.StudentService;
import com.example.xiaoyuan.utils.MD5;
import com.example.xiaoyuan.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 根据id删除单个或多个学生信息
     * @param ids
     * @return
     */
    @DeleteMapping("/delStudentById")
    public Result delStudentById (@RequestBody List<Integer> ids){

        studentService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 添加学生信息和修改学生信息
     * @param student
     * @return
     */
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@RequestBody Student student){
        Integer id = student.getId();
        if (id == null || id == 0){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    /**
     * 分页获取学生列表
     * @param pageNo
     * @param pageSize
     * @param student
     * @return
     */
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            Student student){

        Page<Student> pageParam = new Page<>(pageNo,pageSize);
        IPage<Student> studentIPage = studentService.getStudentByOpr(pageParam, student);
        return Result.ok(studentIPage);
    }


}
